import React, { Component } from 'react';
import { Input, Button } from 'react-native-elements';
export default class SendCodeButton extends Component {
    constructor(props) {
        super(props);
    
        this.state = {
            timerCount:60,
            disabled:false,
            title:'发送验证码',
        };
      }
    _shouldStartCountting = (shouldStart,disabled)=> {
        if(shouldStart){
            this.setState({
                disabled:true,
                title:`重新获取(${this.state.timerCount})`,
            });
            this.timer = setInterval(() => {
                this.setState({
                    disabled:true,
                    timerCount:this.state.timerCount-1,
                    title:`重新获取(${this.state.timerCount})`,
                });
                if(this.state.timerCount==0){
                    this.setState({
                        disabled:false,
                        timerCount:60,
                        title:`重新获取`,
                    });
                    clearInterval(this.timer);
                }
           },1000);
        }
        else{
            this.setState({disabled });
        }
    }
    componentWillUnmount() {
        this.timer && clearInterval(this.timer);
    }
    render() {
        return (
            <Button 
                type="outline"
                title={this.state.title}
                onPress={()=>this.props.onPress(this._shouldStartCountting) } 
                disabled = {this.state.disabled}
                titleStyle={{ fontWeight: 'bold', color: 'white' }}
                buttonStyle={{
                    backgroundColor: 'transparent',
                    borderWidth: 1,
                    borderColor: 'white',
                    borderRadius: 10,
                  }}
            />
        );
    }
}